﻿using System;
using System.Diagnostics;

using Kannon.Core;

namespace Kannon.Middleware
{
    public class RequestLoggerMiddleware : KannonMiddleware
    {
        public RequestLoggerMiddleware()
        {
        }

        public override KannonResponse Handle(KannonRequest request)
        {
            Console.WriteLine("[{0:HH:mm:ss.fff}] {1} {2}", DateTime.UtcNow, request.HttpMethod, request.Url);

            var stopwatch = Stopwatch.StartNew();
            var response = Application.Handle(request);

            Console.WriteLine("\t{0} {1} ({2} sec., {3} bytes)", 
                (int)response.StatusCode, 
                response.StatusDescription, 
                stopwatch.Elapsed,
                response.Headers["Content-Length"]);

            return response;
        }
    }
}
